package com.bilibili.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bilibili.dao.BaseDao;
import com.bilibili.dao.UserDao;
import com.bilibili.dao.impl.UserDaoImpl;
import com.bilibili.entity.Follow;
import com.bilibili.entity.Page;
import com.bilibili.entity.User;
import com.bilibili.service.UserService;

public class UserServiceImpl implements UserService {
	
	UserDao userDao = new UserDaoImpl();
	
	@Override
	public User queryUserByUserId(int userId) {
		// TODO Auto-generated method stub
		return userDao.queryUserByUserId(userId);
	}
	
	@Override
	public Page<User> queryAllMyAttention(User user,int pageNum,int pageSize) {
		// TODO Auto-generated method stub
		List<User> userList = userDao.queryAllMyAttention(user,pageNum,pageSize);
		int totalRows = userDao.queryAllMyAttentionNum(user);
		Page<User> UserPage = new Page<User>(userList,pageNum,pageSize,totalRows);
		return UserPage;
	}
	
	@Override
	public Page<User> queryAllAttentionToMe(User user,int pageNum,int pageSize) {
		// TODO Auto-generated method stub
		List<User> userList = userDao.queryAllAttentionToMe(user,pageNum,pageSize);
		int totalRows = userDao.queryAllAttentionToMeNum(user);
		Page<User> UserPage = new Page<User>(userList,pageNum,pageSize,totalRows);
		return UserPage;
	}

	@Override
	public User queryFollowIsMutual(Follow follow) {
		// TODO Auto-generated method stub
		return userDao.queryFollowIsMutual(follow);
	}

	@Override
	public boolean updataHeadImg(User user) {
		// TODO Auto-generated method stub
		return userDao.updataHeadImg(user) == 1;
	}
	@Override
	public int userRegister(User user) {
		if(userDao.selectUserByUsername(user.getUsername())!=null) {
			return -1;
		}
		return userDao.insertUser(user);
	}
	@Override
	public Map<String, Object> userLogin(String username, String password) {
		Map<String,Object> map = new HashMap();
		User loginUser = userDao.selectUser(username, password);
		if(loginUser != null) {
			map.put("loginUser",loginUser);
		}else {
			loginUser = userDao.selectUserByUsername(username);
			if(loginUser == null) {
				map.put("msg","用户名不存在");
			}else {
				map.put("msg","密码错误");
			}
		}
		return map;
	}
	
	@Override
    public User getUserById(int userId) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        String sql = "SELECT user_id, username, avatar_url " +
                     "FROM user WHERE user_id = ?";
        try {
            con = BaseDao.getConn();
            ps = con.prepareStatement(sql);
            ps.setInt(1, userId);
            rs = ps.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUsername(rs.getString("username"));
                user.setAvatarUrl(rs.getString("avatar_url"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(con, ps, rs);
        }
        return user;
    }

	UserDao ud = new UserDaoImpl();
	@Override
	public Page<User> SelectAllUser(int pageNum,int pageSize) {
		// TODO Auto-generated method stub
		List<User> books= ud.SelectAllUser(pageNum, pageSize);
		int totalRows = ud.selectAllUserNum();
		Page<User> page = new Page(books,pageNum,pageSize,totalRows);
		return page;
	}
	@Override
	public int updateByUId(int userId) {
		// TODO Auto-generated method stub
		return ud.updateByUId(userId);
	}
	@Override
	public int reupdateByUId(int userId) {
		// TODO Auto-generated method stub
		return ud.reupdateByUId(userId);
	}
	@Override
	public int resetCode(int userId) {
		// TODO Auto-generated method stub
		return ud.resetCode(userId);
	}
	@Override
	public List<User> SelectUserByType(int type) {
		// TODO Auto-generated method stub
		return ud.SelectUserByType(type);
	}
	@Override
	public List<User> SelectUserByRole(int role,int type) {
		// TODO Auto-generated method stub
		return ud.SelectUserByRole(role,type);
	}
	@Override
	public List<User> SelectUserByUserId(int userId) {
		// TODO Auto-generated method stub
		return ud.SelectUserByUserId(userId);
	}

	@Override
	public User selectUpAllInfo(int videoId) {
		// TODO Auto-generated method stub
		return ud.selectUpAllInfo(videoId);
	}
}
